﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;

namespace Baubixinh_client
{
    public partial class frmAttribute : DevExpress.XtraEditors.XtraForm
    {
        private DataTable mobjAttr;
        private DataTable mobjAttrVal;
        private DataTable mobjCuaHang;
        private int mintAttr;
        private int mintAttrVal;
        private bool mblnAddMode;

        public frmAttribute()
        {
            InitializeComponent();
        }

        private void frmAttribute_Load(object sender, EventArgs e)
        {
            try
            {
                //lay gia tri attr
                mobjAttr = clsDbAccess.fncGetAttribute(Program.gobjDb);
                mobjCuaHang = clsDbAccess.fncGetAttributeValue(Program.gobjDb, clsEnum.ATTRIBUTE.STORE);

                grdControlAttribute.DataSource = mobjAttr;
                rpeCuaHang.DataSource = mobjCuaHang;
                cbCuaHang.Properties.DataSource = mobjCuaHang;
                cbNewCuaHang.Properties.DataSource = mobjCuaHang;

                //disable cac nut
                cbCuaHang.Visible = false;
                cbNewCuaHang.Visible = false;
                lblCuaHang.Visible = false;
                lblNewCuaHang.Visible = false;

                //disable add
                grpAdd.Enabled = false;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void dgvAttribute_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                //lay attr dang duoc chon
                mintAttr = clsCommon.fncCnvNullToInt(dgvAttribute.GetFocusedRowCellValue(colAttr_id));

                //fill grid
                mobjAttrVal = clsDbAccess.fncGetAttributeValue(Program.gobjDb, (clsEnum.ATTRIBUTE)mintAttr);
                grdControlAttributeValue.DataSource = mobjAttrVal;

                //an-hien cot cua hang
                colCuaHang.Visible = false;
                cbCuaHang.Visible = false;
                cbNewCuaHang.Visible = false;
                lblCuaHang.Visible = false;
                lblNewCuaHang.Visible = false;
                if (mintAttr == (int)clsEnum.ATTRIBUTE.STAFF)
                {
                    colCuaHang.Visible = true;
                    cbCuaHang.Visible = true;
                    cbNewCuaHang.Visible = true;
                    lblCuaHang.Visible = true;
                    lblNewCuaHang.Visible = true;
                }

                //clear value
                mblnAddMode = false;
                txtOrder.Value = 0;
                txtValue.Text = "";
                cbNewCuaHang.EditValue = null;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void dgvAttributevalue_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                string strName;
                int intOrder;
                int intCuaHang;

                //lay value dang duoc chon
                mintAttrVal = clsCommon.fncCnvNullToInt(dgvAttributevalue.GetFocusedRowCellValue(colValue_id));
                strName = clsCommon.fncCnvNullToString(dgvAttributevalue.GetFocusedRowCellValue(colAttributeValue_name));
                intOrder = clsCommon.fncCnvNullToInt(dgvAttributevalue.GetFocusedRowCellValue(colOrder));
                intCuaHang = clsCommon.fncCnvNullToInt(dgvAttributevalue.GetFocusedRowCellValue(colCuaHang));

                //fill textbox
                txtValue.Text = strName;
                txtOrder.Value = intOrder;
                cbCuaHang.EditValue = intCuaHang;

                //enable cac nut
                btnAdd.Enabled = true;
                btnNewCancel.Enabled = true;
                btnSave.Enabled = true;

                mblnAddMode = false;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                grpAttr.Enabled = false;
                grpAttrValue.Enabled = false;
                grpEdit.Enabled = false;
                grpAdd.Enabled = true;

                txtNewValue.Focus();
                mblnAddMode = true;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int intAttrID;
                int intValueID;
                string strName;
                int intOrder;
                int intCuaHang;

                if (!mobjValid.Validate()) return;

                if (!clsCommon.fncMsgConfirm("Dữ liệu sẽ được lưu, bạn có chắc chắn?")) return;

                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                intAttrID = clsCommon.fncCnvNullToInt(dgvAttribute.GetFocusedRowCellValue(colAttr_id));
                intValueID = clsCommon.fncCnvNullToInt(dgvAttributevalue.GetFocusedRowCellValue(colValue_id));
                strName = txtValue.Text.Trim();
                intOrder = clsCommon.fncCnvNullToInt(txtOrder.Value);
                intCuaHang = clsCommon.fncCnvNullToInt(cbCuaHang.EditValue);

                if (clsDbAccess.fncUpdateAttributeValue(Program.gobjDb, Program.gobjTrans, intValueID, strName, intCuaHang, intOrder))
                {
                    Program.gobjTrans.Commit();
                    clsCommon.fncMsgInfo("Cập nhật thuộc tính thành công.");

                    //load lai du lieu
                    dgvAttribute_FocusedRowChanged(null, null);
                }
                else
                {
                    Program.gobjTrans.Rollback();
                    clsCommon.fncMsgError("Cập nhật thuộc tính thất bại.");
                }

            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                if (!clsCommon.fncMsgConfirm("Bạn có chắc muốn hủy thay đổi?")) return;

                //xoa control
                txtNewValue.Text = "";
                txtOrder.Value = 0;
                cbNewCuaHang.EditValue = null;

                //enable group
                grpEdit.Enabled = true;
                grpAttr.Enabled = true;
                grpAttrValue.Enabled = true;
                grpAdd.Enabled = false;

                if (dgvAttributevalue.FocusedRowHandle < 0) return;
                dgvAttributevalue_FocusedRowChanged(null, null);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnNewSave_Click(object sender, EventArgs e)
        {
            try
            {
                int intAttrID;
                int intValueID;
                string strName;
                int intOrder;
                int intCuaHang;

                if (!mobjNewValid.Validate()) return;

                if (!clsCommon.fncMsgConfirm("Dữ liệu sẽ được lưu, bạn có chắc chắn?")) return;

                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                intAttrID = clsCommon.fncCnvNullToInt(dgvAttribute.GetFocusedRowCellValue(colAttr_id));
                intValueID = clsCommon.fncCnvNullToInt(dgvAttributevalue.GetFocusedRowCellValue(colValue_id));
                strName = txtNewValue.Text.Trim();
                intOrder = clsCommon.fncCnvNullToInt(txtNewOrder.Value);
                intCuaHang = clsCommon.fncCnvNullToInt(cbNewCuaHang.EditValue);

                if (clsDbAccess.fncInsertAttributeValue(Program.gobjDb, Program.gobjTrans, intAttrID, strName, intCuaHang, intOrder))
                {
                    Program.gobjTrans.Commit();
                    clsCommon.fncMsgInfo("Thêm mới thuộc tính thành công.");

                    //load lai du lieu
                    dgvAttribute_FocusedRowChanged(null, null);

                    //xoa control
                    txtNewValue.Text = "";
                    txtNewOrder.Value = 0;
                    cbNewCuaHang.EditValue = null;

                    //enable group
                    grpEdit.Enabled = true;
                    grpAttr.Enabled = true;
                    grpAttrValue.Enabled = true;
                    grpAdd.Enabled = false;
                }
                else
                {
                    Program.gobjTrans.Rollback();
                    clsCommon.fncMsgError("Thêm mới thuộc tính thất bại.");
                }

            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }
    }
}